package com.liveneo.adapter.mobile.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.liveneo.adapter.base.entity.PrpmCaseInfo;
import com.liveneo.adapter.base.entity.PrpmLoggerInfo;
import com.liveneo.adapter.base.entity.PrpmLossItem;
import com.liveneo.adapter.base.mapper.PrpmCaseInfoMapper;
import com.liveneo.adapter.base.mapper.PrpmLossItemMapper;
import com.liveneo.adapter.mobile.model.PrpmCountReupload;
import com.liveneo.adapter.mobile.model.dto.PrpmBoardCaseInfoDto;
import com.liveneo.adapter.mobile.model.dto.PrpmCaseInfoDto;
import com.liveneo.adapter.mobile.model.dto.PrpmEndCaseInfoDto;
import com.liveneo.adapter.mobile.model.dto.PrpmHesunDetailDto;
import com.liveneo.adapter.mobile.model.dto.PrpmImgCheckDetailDto;
import com.liveneo.adapter.mobile.model.dto.PrpmRealCaseInfoDto;
import com.liveneo.adapter.mobile.model.dto.PrpmReportCaseInfoDto;
import com.liveneo.adapter.mobile.model.dto.PrpmUserStatusDto;
import com.liveneo.adapter.mobile.model.dto.VsResponseHeadDto;
import com.liveneo.adapter.mobile.model.request.LossItemRequest;
import com.liveneo.adapter.mobile.service.CaseInfoService;
import com.liveneo.system.base.utils.Str;

@Service
@SuppressWarnings("rawtypes")
public class CaseInfoServiceImpl implements CaseInfoService {
	@Autowired
	private PrpmCaseInfoMapper prpmCaseInfoMapper;

	@Autowired
	private PrpmLossItemMapper prpmLossItemMapper;

//	@Autowired
//	private PrpmLossItemHistoryService prpmLossItemHistoryService;
//
//	@Autowired
//	private LossItemService lossItemService;

	@Override
	public List<PrpmCaseInfo> getCaseInfoList(List record) {
		return prpmCaseInfoMapper.selectByCaseNo(record);
	}

	@Override
	public List<PrpmCaseInfo> selectRealAllCaseInfo(PrpmCaseInfoDto prpmCaseInfoDto) {
		return prpmCaseInfoMapper.selectByBean(prpmCaseInfoDto);
	}

	@Override
	public PrpmCaseInfo getCaseInfo(String caseNo) {
		return prpmCaseInfoMapper.selectCaseInfoByCaseNo(caseNo);
	}

	@Override
	public PrpmCaseInfo getCaseInfo(LossItemRequest request) {
		return prpmCaseInfoMapper.selectCaseInfoByRequest(request);
	}

	@Override
	public int updateByPrimaryKey(PrpmCaseInfo record) {

		return prpmCaseInfoMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<PrpmCaseInfo> getCaseInfoList(int currentPage, int pagesize, ArrayList<String> caseNoList) {
		return null;
	}

	@Override
	public List<PrpmCaseInfo> selectAllCaseInfo(int pageNum, int pageSize, PrpmCaseInfoDto caseInfo) {
		PageHelper.startPage(pageNum, pageSize);
		return prpmCaseInfoMapper.selectByBean(caseInfo);
	}

	@Override
	public int selectByReUploadFlag(Integer reUploadFlag) {
		return prpmCaseInfoMapper.selectByReUploadFlag(reUploadFlag);
	}

	@Override
	public PrpmCaseInfo selectByPrimaryKey(String caseInfoId) {
		return prpmCaseInfoMapper.selectByPrimaryKey(caseInfoId);
	}

	@Override
	public List<PrpmCaseInfo> getCaseInfo(List record) {
		return prpmCaseInfoMapper.selectByUuidList(record);
	}

	@Override
	public List<PrpmCaseInfo> selectByUuidListCaseStatus(List record) {
		return prpmCaseInfoMapper.selectByUuidListCaseStatus(record);
	}

	@Override
	public List<PrpmCaseInfo> selectCostCaseInfo(PrpmCaseInfoDto caseInfoDto) {
		return prpmCaseInfoMapper.selectByBean(caseInfoDto);
	}

	@Override
	public int getCaseInfoCount(PrpmCaseInfoDto caseInfoDto) {
		return prpmCaseInfoMapper.caseInfoCount(caseInfoDto);
	}

	@Override
	public int delete(String caseNo) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.deleteByCaseNo(caseNo);
	}

	@Override
	public int save(PrpmCaseInfo record) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.insert(record);
	}

	@Override
	public List<PrpmCaseInfo> selectByUuidAndCaseStatusDown(List caseUuids, String pageSize, String pageNo)
			throws Exception {
		// TODO Auto-generated method stub
		PageHelper.startPage(Integer.parseInt(Str.chgStrZero(pageNo)), Integer.parseInt(Str.chgStrZero(pageSize)));
		return prpmCaseInfoMapper.selectByUuidAndCaseStatusDown(caseUuids);
	}

	@Override
	public List<PrpmCaseInfo> selectByUuidAndCaseStatusUp(List caseUuids, String pageSize, String pageNo)
			throws Exception {
		// TODO Auto-generated method stub
		PageHelper.startPage(Integer.parseInt(Str.chgStrZero(pageNo)), Integer.parseInt(Str.chgStrZero(pageSize)));
		return prpmCaseInfoMapper.selectByUuidAndCaseStatusUp(caseUuids);
	}

	@Override
	public List<PrpmEndCaseInfoDto> selectEndCaseByBean(int pageNum, int pageSize, PrpmEndCaseInfoDto endCase) {
		PageHelper.startPage(pageNum, pageSize);
		return prpmCaseInfoMapper.selectEndCaseByBean(endCase);
	}

	@Override
	public List<PrpmRealCaseInfoDto> selectRealCaseInfoByBean(int pageNum, int pageSize,
			PrpmRealCaseInfoDto realCaseInfo) {
		PageHelper.startPage(pageNum, pageSize);
		return prpmCaseInfoMapper.selectRealCaseInfoByBean(realCaseInfo);
	}

	@Override
	public List<PrpmEndCaseInfoDto> selectAllEndCaseByBean(PrpmEndCaseInfoDto exportEndCase) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.selectEndCaseByBean(exportEndCase);
	}

	@Override
	public List<PrpmRealCaseInfoDto> selectRealCaseInfoByBean(PrpmRealCaseInfoDto realCaseInfo) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.selectRealCaseInfoByBean(realCaseInfo);
	}

	@Override
	public List<PrpmUserStatusDto> selectMobileUserStatus(List<String> uuidList) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.selectMobileUserStatus(uuidList);
	}

	@Override
	public List<PrpmUserStatusDto> selectCheckUserStatus(List<String> uuidList) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.selectCheckUserStatus(uuidList);
	}

	@Override
	public List<PrpmUserStatusDto> selectAllCompanyCaseStatus(List<String> uuidList) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.selectAllCompanyCaseStatus(uuidList);
	}

	@Override
	public List<PrpmBoardCaseInfoDto> selectboardCaseInfo(int pageNum, int pageSize, PrpmCaseInfoDto caseInfoDto) {
		PageHelper.startPage(pageNum, pageSize);
		return prpmCaseInfoMapper.selectboardCaseInfo(caseInfoDto);
	}

	@Override
	public List<PrpmHesunDetailDto> selectHesunUserDetail(int pageNum, int pageSize, PrpmHesunDetailDto hesunDetail) {
		PageHelper.startPage(pageNum, pageSize);
		return prpmCaseInfoMapper.selectHesunUserDetail(hesunDetail);
	}

	@Override
	public List<String> selectAllCaseUuid(List<String> uuidList) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.selectAllCaseUuid(uuidList);
	}

	@Override
	public List<PrpmImgCheckDetailDto> selectImgCaseDetail(String imgCheck_caseUuid) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.selectImgCaseDetail(imgCheck_caseUuid);
	}

	@Override
	public PrpmCountReupload selectCountReupload(List<String> uuidList) {
		return prpmCaseInfoMapper.selectCountReupload(uuidList);
	}

	@Override
	public List<PrpmCaseInfo> selectByUuidAndCaseStatus(List<String> uuids, int caseStatus) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.selectByUuidAndCaseStatus(uuids, caseStatus);
	}

	@Override
	public void imageCheckSuccess(String caseUuid) throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public void updateReCaseStatus(String uuid) throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public void updateCaseInfoFromClaim(PrpmCaseInfo record) {
		// TODO Auto-generated method stub
	}

	@Override
	public void uploadImgsByCaseNo(String caseNo) throws Exception {
		// TODO Auto-generated method stub
	}

	@Override
	public List<PrpmReportCaseInfoDto> selectReportNum() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addLoggerInfo(PrpmLoggerInfo prpmloggerinfo) {
		// TODO Auto-generated method stub
	}

	@Override
	public VsResponseHeadDto autoCollectDoc(String registNo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PrpmCaseInfo> getCaseInfoByUuidOrTime(Map<String, Object> params) {
		return prpmCaseInfoMapper.getCaseInfoByUuidOrTime(params);
	}

	@Override
	public PrpmCaseInfo getCaseInfoByCaseNoAndUuid(String caseNo, String caseUuid) {
		return prpmCaseInfoMapper.selectCaseInfoByCaseNoAndUuid(caseNo, caseUuid);
	}
	
	@Override
	public int updateByPrimaryKeySelective(PrpmCaseInfo record) {
		// TODO Auto-generated method stub
		return prpmCaseInfoMapper.updateByPrimaryKeySelective(record);
	}
	/**
	 * @Title: updateCaseInofStateAndBill @Description: 计费状态及规则更新 @param @param
	 * caseNo @param @return @param @throws Exception 设定文件 @return 返回类型 @throws
	 */
	@Override
	public int updateCaseInofStateAndBill(String caseNo) throws Exception {
		// 查询案件信息
		PrpmCaseInfo caseInfo = prpmCaseInfoMapper.selectCaseInfoByCaseNo(caseNo);
		if(caseInfo == null){
			throw new RuntimeException("案件信息错误，案件号："+caseNo);
		}
		// 查询案件下所有任务
		List<PrpmLossItem> lossItems = prpmLossItemMapper.selectByCaseNo(caseNo);
		boolean isStatus = true;  // 任务状态是否完成
		boolean isBill = false;   // 收费规则
		boolean caseDeal = false; // 任务处理方式
		// 设置 任务状态 2：取证完成 4：改派完成 5：取证前注销 6：取证后注销
		Set<Integer> setLossItemStatus = new HashSet<Integer>();
		setLossItemStatus.add(2);
		setLossItemStatus.add(4);
		setLossItemStatus.add(5);
		setLossItemStatus.add(6);
		for (PrpmLossItem prpmLossItem : lossItems) {
			// 检测所有任务是否全部完成
			if (!setLossItemStatus.contains(prpmLossItem.getLossStatus())) {
				isStatus = false;
			}

			// 客户类型 3:客户,2:查勘员,1:修理厂
			if (prpmLossItem.getUserType() == 3) {
				isBill = true;
			}

			// 检测是否有做过视频或拍照查勘
			if (!"0".equals(prpmLossItem.getLossDealType())) {
				caseDeal = true;
			}
		}

		if (isStatus && lossItems.size() > 0) {
			// 收费状态 0：待处理 1：处理中 2：处理完成
			caseInfo.setCaseStatus(2);
		}

		// 未做过任何视频和拍照查勘
		if (!caseDeal) {
			caseInfo.setBillingType("0"); // 不收费
		} else {
			// 收费规则 4：C 端收费 3：AB端收费
			String billStatus = isBill ? "4" : "3";
			// C端优先
			if (!"4".equals(caseInfo.getBillingType())) {
				caseInfo.setBillingType(billStatus);
			}
		}
		caseInfo.setModifyTime(new Date());
		return prpmCaseInfoMapper.updateByPrimaryKeySelective(caseInfo);
	}

}
